import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**This class would laod the csv file,and construct transaction object.
 * all the transaction will form a list.
 * Also, we would count the occurrence of each item, add it to pathMap.
 * */
public class DataLoader {
	
	public static void load(String file,List<Transaction> transaction, Map<Integer, List<Itemset>> pathMap) throws IOException{
		
		BufferedReader br=null;
		System.out.println("loading data.....");
		
		Map<String, Integer> itemCount=new HashMap<String, Integer>();
		
		List<Itemset> itemList=new ArrayList<Itemset>();
		//TreeSet<Itemset> itemSet=new TreeSet<Itemset>();
		try {
			 br=new BufferedReader(new FileReader(file));
			 
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int id=0;
		String s=null;
		while((s=br.readLine())!=null){
			
			TreeSet<String> rowSet=new TreeSet<String>();
			s=s.trim();// we do not want space and \n affects the result
			String[] arr=s.split(","); // comma seperator
			
			for(int i=0;i<arr.length;i++){// count the occurence of each item
				String tmp=arr[i].trim();
				if(tmp.equals("")){
					continue;
				}	
				if(itemCount.containsKey(tmp)){
					int count=itemCount.get(tmp);
					itemCount.put(tmp, count+1);
				}else{
					
					itemCount.put(tmp, 1);
				}
				rowSet.add(tmp);// we treat itemset as a market basket.
				
			}
			
			Transaction tr=new Transaction(id, rowSet);
			transaction.add(tr);
			id++;
		}
		for(String item:itemCount.keySet()){	
			Itemset itemcount=new Itemset(item, itemCount.get(item));
			itemList.add(itemcount);
		}
		pathMap.put(1, itemList);	
	//	for(Itemset iSet:itemList){
	//		System.out.println(iSet);
	//	}
		
	}
		
}
